<template>
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="auto" :size="formSize" status-icon >
        <el-form-item label="原因说明" prop="reason" required>
            <el-input v-model="ruleForm.reason" type="textarea" :autosize="{ minRows: 4 }" />
        </el-form-item>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../../../../util/request';
const loading = ref<boolean>(false)

interface RuleForm {
    id:number
    type: string
    model: string
    source_id: number
    source:string
    source_type: string
    reason: string
    price: number
}
const material = ref<any>({})
const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id:0,
    type:'',
    model:'',
    source:'Reimburse',
    source_id: 0,
    source_type: 'material',
    reason: '/',
    price: 0,
})

const onGetData = () => {
  
}

const props = defineProps<{
    data:any,
    type:string
    model:string
}>()

watch(props,()=>{
    ruleForm.value.source_id = props.data
    ruleForm.value.type = props.type
    ruleForm.value.model = props.model
    ruleForm.value.reason = '/',
    ruleForm.value.price = 0,
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    reason: [
        { required: true, message: '请输入付款原因说明', trigger: 'blur' }
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            ruleForm.value.price = material.value.price
            http.put('/approve',ruleForm.value).then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
</style>
